package com.vaadin.lab3;

import com.vaadin.Application;
import com.vaadin.lab3.LongRunningOperation.FinishEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

@SuppressWarnings("serial")
public class Lab extends Application implements
        LongRunningOperation.FinishedListener {

    private HorizontalLayout mainLayout = new HorizontalLayout();
    
    LongRunningOperation operation = new LongRunningOperation();

	private Label label;
	

    @Override
    public void init() {
        mainLayout.setSpacing(true);
        mainLayout.setMargin(true);
        Window mainWindow = new Window("Lab", mainLayout);
        setMainWindow(mainWindow);
     
        operation.addListener(this);
        
        Button button = new Button("start");
		mainWindow.addComponent(button);
		button.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {

				label.setValue("Running ...");
				operation.start();
			}
		});
		
		button = new Button("start async");
		mainWindow.addComponent(button);
		button.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				label.setValue("Running ...");
				operation.startAsync();
				
			}
		});
		
		final ProgressIndicator indicator = new ProgressIndicator();
		indicator.setPollingInterval(500);
		
		label = new Label("State"); 
		mainWindow.addComponent(label);
    }

    @Override
    public void finished(FinishEvent event) {
        synchronized (this) {
        	label.setValue("Finished.");
        }
    }
}
